package online.lmds.sdmanage.common.entity;

/**
 * Created by Brighten Ma on 2017/4/30.
 */
public class Page {
    // pageSize 默认为 25
    private final static Integer PAGE_SIZE = 25;

    // 一页的个数
    private Integer pageSize;

    // 跳过多少行
    private Integer offset;

    // 欲显示的页码
    private Integer pageNumber;

    public Page() {
        pageSize = PAGE_SIZE;
    }

    public Page(Integer pageSize) {
        this.pageSize = (pageSize == null || pageSize < 1 || pageSize > 100) ? PAGE_SIZE : pageSize;
    }

    public Page(Integer pageSize, Integer defaultPageSize) {
        this.pageSize = (pageSize == null || pageSize < 1 || pageSize > 100) ? defaultPageSize : pageSize;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public Integer getPageNumber() {
        return pageNumber;
    }

    public void setPageNumber(Integer pageNumber) {
        this.pageNumber = (pageNumber == null || pageNumber < 1) ? 1 : pageNumber;
        this.offset = (this.pageNumber - 1) * this.pageSize;
    }

    public Integer getOffset() {
        return offset;
    }

    /**
     * 返回总页数
     * @param totalCount 总个数
     * @return 页数
     */
    public Integer getTotalPages(Integer totalCount) {
        if (totalCount == null || totalCount <= 0) {  // 数量为0，返回总页数0
            return 0;
        }
        int fullPages = totalCount / pageSize;
        int lastCount = totalCount % pageSize;
        return fullPages + (lastCount == 0 ? 0 : 1);
    }
}
